#import "MathGame.h"

@interface MathGame ()

@property int deviation;
@property BOOL isGameOver;
@property NSArray* colors;
@property NSMutableArray* characters;

@end


@implementation MathGame


- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setName:@"MathGame"];
        [self setType:knowleadge];
        self.ruleOfTheGame=@"Hit when equation is correct";
        self.characters=[[NSArray alloc]initWithObjects:@"+",@"-",@"*", nil];
        self.colors=[[NSArray alloc]initWithObjects:@"blue",@"white",@"red",@"yellow",@"orange",@"purple",@"green", nil];
        
        
    }
    return self;
}

-(void)draw{
    [self removeAllComponents];
    
    int firstNumber=arc4random_uniform(kMathGameMaxNumber);
    int secondNumber=arc4random()%kMathGameMaxNumber;
    int character=arc4random_uniform(kMathGameCharacterNumber);
    self.deviation=arc4random_uniform(kMathGameMaxDivertion);
    int colour=arc4random_uniform(kColorNumber);
    int  rightValue;
    if (character==0) {
        rightValue=firstNumber + secondNumber;
    }
    if (character==1) {
        rightValue=firstNumber - secondNumber;
    }
    if (character==2) {
        if (firstNumber<=kMathGameMaxMakeEasyNumber || secondNumber <= kMathGameMaxMakeEasyNumber) {
            rightValue=firstNumber * secondNumber;
        }
        else{
            character=0;
            rightValue=firstNumber+secondNumber;
        }
    }


    
    NSString* labelText=[NSString stringWithFormat:@"%d %@ %d = %d",firstNumber,[self.characters objectAtIndex:character],secondNumber, rightValue-self.deviation];
    NSString* labelColor=[NSString stringWithFormat:@"%@Color",[self.colors objectAtIndex:colour]];
    
    


    LabelGameComponent* label=[[LabelGameComponent alloc]initWithText:labelText andFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight/kHeighDel) andFontSize:kScreenWidth/kWidthDel andColour:labelColor];
    LabelGameComponent* labelOne=[[LabelGameComponent alloc]initWithText:labelText andFrame:CGRectMake(0, kScreenHeight-kScreenHeight/kHeighDel - 2*kButtonHeigh, kScreenWidth, kScreenHeight/kHeighDel) andFontSize:kScreenWidth/kWidthDel andColour:labelColor];
    
    [self addComponents:label];
    [self addComponents:labelOne];
    
}

-(BOOL)isRight{
    if (self.deviation==0) {
        return YES;
        
    }
    else{
        return NO;
    }
}

-(void)play{
    [self draw];
    
    NSTimer *timer1 = [NSTimer scheduledTimerWithTimeInterval: 2.5                                                 target: self selector:@selector(newNumber:)userInfo: nil repeats:YES];
    
}

-(void)newNumber:(NSTimer*)timer{
    if (self.isGameOver==YES) {
        [timer invalidate];
    }
    else{
        [self draw];
        
        [[NSNotificationCenter defaultCenter]postNotificationName:@"SomeGames"
         object:self userInfo:nil];
        
    }
    
}

-(void)gameOver{
    self.isGameOver=YES;
}


@end
